package server.main;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

import server.mobiles.Mobile;
import server.mobiles.PlayerMobile;
import server.utility.PacketId.PacketID;


public class PacketHandler implements Runnable{

	private Socket socket;
	private BufferedReader reader;
	private DataOutputStream writer;
	
	public PacketHandler(Socket s) throws IOException{
		socket=s;
		reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		writer= new DataOutputStream(socket.getOutputStream());
	}
	
	public void run() {
		PacketID id;
		String line;
		try{
			while(true){
				line=reader.readLine();
				id=PacketID.get(line);
				if(id!=PacketID.none)
					break;
			}
			
			switch(id){
				case createPg:{
					String name=line.replace(id.toString(), "");
					
					if(!Data.hasUser(name)){
						PlayerMobile pm=PlayerMobile.create(name);
						if(pm!=null){
							System.out.println("- player "+pm.getName()+" created");
							login(pm);
							return;
						}
					}
					else{
						writer.write("user exists".getBytes());
						socket.close();
						return;
					}
				}
				case login:{
					PlayerMobile pm=Data.getUser(line.replace(id.toString(), ""));
					
					if(pm!=null){
						login(pm);
						return;
					}
					else{
						writer.write("user inexistend".getBytes());
						socket.close();
						return;
					}
				}
			}
		}catch (IOException e) {e.printStackTrace();}
	}
	
	private void login(PlayerMobile pm) throws IOException{
		String line;
		
		System.out.println("- player "+pm.getName()+" login");
		pm.login();
		
		writer.write(pm.toString().getBytes());
		for(Mobile m: pm.getVisibleMobile()){
			if(pm==m)
				continue;
			
			writer.write(m.toString().getBytes());
		}
					
		writer.write("\n".getBytes());
		
		while(true){
			line=reader.readLine();
			PacketID id=PacketID.get(line);
			
			switch(id){
				case mobsDetails:{
					Mobile mob=Data.getMobile(line.replace(id.toString(), ""));
					
					writer.write(mob.toString().getBytes());
					break;
				}
				case logout:{
					PlayerMobile pl=Data.getUser(line.replace(id.toString(), ""));
					
					if(pl!=null){
						System.out.println("- player "+pl.getName()+" logout");
						pl.logout();
						
					}
					
					socket.close();
					return;
				}
				case userAction:{
					String[] first = line.replace(id.toString(), "").split(";");
					
					PlayerMobile user = Data.getUser(first[1]);
					
					if(first[0].equals("up"))
						user.moveUp();
					else if(first[0].equals("down"))
						user.moveDown();
					else if(first[0].equals("left"))
						user.moveLeft();
					else if(first[0].equals("right"))
						user.moveRight();
					else if(first[0].equals("jump"))
						user.jump();
					
					break;
				}
				case position:{
					line=Data.getMobPositions(line.replace(id.toString(), ""));
					writer.write(line.getBytes());
					
					break;
				}
			}
		}
	}

}
